package com.samphanie.auiu.common.ui.view

import android.content.Context
import android.graphics.Typeface
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.StringDef
import androidx.annotation.StringRes
import androidx.databinding.ViewDataBinding
import com.samphanie.auiu.common.R
import com.samphanie.auiu.common.databinding.LayoutEmptyViewBinding

/**
 * @author ZSY
 */
class EmptyView : LinearLayout {

    private var dataBinding: LayoutEmptyViewBinding

    constructor(context: Context) : this(context, null) {}

    constructor(context: Context, attributes: AttributeSet?) : this(context, attributes, 0) {}

    constructor(context: Context, attributes: AttributeSet?, defstyle: Int) : super(
        context,
        attributes,
        defstyle
    ) {
        orientation = VERTICAL
        gravity = Gravity.CENTER

        dataBinding = LayoutEmptyViewBinding.inflate(LayoutInflater.from(context), this, true)
    }

    fun setIcon(@StringRes iconRes: Int) {
        dataBinding.emptyIcon.setText(iconRes)
    }

    fun setTitle(text: String) {
        dataBinding.emptyTitle.text = text
        dataBinding.emptyTitle.visibility = if (text.isBlank()) View.GONE else View.VISIBLE
    }

    fun setDesc(text: String) {
        dataBinding.emptyDesc.text = text
        dataBinding.emptyDesc.visibility = if (text.isBlank()) View.GONE else View.VISIBLE
    }

    @JvmOverloads
    fun setHelpAction(actionId: Int = R.string.if_detail, listener: OnClickListener) {
        dataBinding.emptyTips.setText(actionId)
        dataBinding.emptyTips.setOnClickListener(listener)
        dataBinding.emptyTips.visibility = VISIBLE

        if (actionId == -1) {
            dataBinding.emptyTips.visibility = GONE
        }
    }

    fun setButton(text: String, listener: OnClickListener) {
        if (text.isBlank()) {
            dataBinding.emptyAction.visibility = GONE
        } else {
            dataBinding.emptyAction.visibility = View.VISIBLE
            dataBinding.emptyAction.text = text
            dataBinding.emptyAction.setOnClickListener(listener)
        }
    }
}